<html>
<head>
<#assign basePath=request.contextPath>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>后台管理系统</title>
    <!-- Bootstrap Core CSS -->
<#include "/WEB-INF/views/template/bootMainCss.ftl" parse=true encoding="utf-8">
    <style>
        .form-group{ margin-bottom: 0;}
        .verify-tip{display: none; margin-top: 5px; margin-bottom: 10px; color: #a94442;font-size: 85%;}
        .to{width:100px;height:100px;border-radius:50%; overflow: hidden; background-color: #cccccc; margin-left: -30px; display: inline-block;}
        .to .input-file,.to .input-file input{ display: block; width: 100%; height: 100%;}
        .to .input-file input{ opacity: 0;}
        #myTable{border: 1px solid #cccccc;}
    </style>
</head>
<body>
<div style="padding: 0 15px;">
    <div class="panel panel-default">
        <div class="panel panel-f5">
            <div class="super-tit">
                <form id="myForm" class="form-inline form-margin-right">
                    <div class="panel-body">
                        <div class="col-xs-4">
                            <div class="form-group">
                                <input type="text" class="form-control" id="key" size="20" placeholder="请输入搜索内容">
                                <button type="button" class="btn btn-primary btn-sm" onclick="search(1)">搜索</button>
                            </div>
                        </div>
                </form>
            </div>
        </div>
        <!-- 分页查询的地址-->
        <div class="super-box panel-white">
            <div class="table-responsive">
                <table id="myTable"
                       class="table table-hover table-border"
                       data-url="${basePath}/admin/manager/json"
                       data-pageSize="10,50,100" data-method="GET">
                    <thead>
                    <!-- 需要循环的字段  对应mapper文件-->
                    <tr>
                        <th  data-field="id">ID</th>
                        <th  data-field="loginName">推荐人姓名</th>
                        <th  data-field="realName" width="15%">推荐人</th>
                        <th  data-field="button" data-call="true">操作</th>
                    </tr>
                    </thead>
                    <tbody id="tbodyId">

                    </tbody>
                </table>
            </div>
            <!-- 分页条-->
            <div class="row">
                <div align="left" class="col-xs-6">
                    <div class="input-group">
                        <span class="input-group-addon" id="pageCount"></span> <select
                            id="pageSize" onchange="search(1)" class="form-control"
                            style="width: 100px"></select>
                    </div>
                </div>
                <div align="right" class="col-xs-6">
                    <ul class="pagination" id="pagination"></ul>
                </div>
            </div>
        </div>
    </div>

<#include "/WEB-INF/views/template/bootMainJs.ftl" parse=true encoding="utf-8">
    <script type="text/javascript" src="${basePath}/bootstrap/plugin/jqPaginator/jqPaginator.js"></script>
    <script type="text/javascript" src="${basePath}/bootstrap/plugin/jqPaginator/myPage.js"></script>
    <script>
        function tableCallBack(data, id) {
            if (id == 'button') {//增加操作按钮
                var _button = '<button type="button" name="'+data.id+'" data-name="'+data.realName+'" class="btn btn-white btn-xs" onclick="selectMeal(this);">选择</button> ';
                return _button;
            }
        }
        //选择
        function selectMeal(obj){
            var userId = obj.name;
            var userName =  $(obj).attr("data-name");
            var memberId = parent.$("#memberId").val();
            if(parent.$("#userList").val() == 1){
                layer.confirm('是否确定此操作？', {
                    btn: ['确定','取消'] //按钮
                }, function() {
                    $.ajax({
                        type: 'POST',
                        url: '${basePath}/admin/manager/update',
                        data:{
                            id: memberId,
                            userId: userId
                        },
                        success: function (data) {
                            console.log(data)
                            var data = JSON.parse(data);
                            if(data.code == "0"){
                                layer.alert('绑定成功!', {
                                    icon: 6
                                }, function(index){
                                    parent.layer.closeAll();
                                });
                            }
                            else{
                                layer.alert(data.desc, {
                                    icon: 5
                                }, function(index){
                                    parent.layer.closeAll();
                                });
                            }
                        }
                    });
                });
            }
            else{
                parent.$("#userPermission").val(userName);
                parent.$("#userId").val(userId);
                parent.layer.closeAll();
            }
        }

        function checkName(){
            var obj = $('#loginName').val();
            var tip = $(".verify-tip");
            if(obj.length > 3){
                $.ajax({
                    type:"GET",
                    url:"${basePath}/admin/manager/checkLoginName",
                    dataType: 'json',
                    data:{
                        loginName:obj
                    },
                    success:function(data){
                        if(data == false){
                            tip.show();
                        }
                        else{
                            tip.hide();
                        }
                    },
                    error:function(){
                        console.log("请求失败")
                    }
                })
            }
        }
        function formSubmit() {
            var bsVal = $("#myForm").data('bootstrapValidator');
            bsVal.validate();
            if (bsVal.isValid()) {
                if ($(".verify-tip:visible").length > 0) {
                    return false
                }
                else {
                    $.ajax({
                        type: 'POST', // You can change this to GET if you want to submit via GET
                        method: $(this).attr('method'),
                        url: '${basePath}/admin/manager/' + action,
                        dataType: 'json',
                        data: {
                            id: $('#id').val(),
                            deptId: $('#deptId').val(),
                            loginName: $('#loginName').val(),
                            realName: $('#realName').val(),
                            sex: $('#sex').val(),
                            phone: $('#phone').val(),
                            email: $('#email').val(),
                            state: $("input[type='radio']:checked").val()
                        },
                        success: function (data) {
                            if (data.code == '0' || data.code == '0000') {
                                layer.alert('操作成功!', {
                                    icon: 6
                                });
                                setTimeout(3000);
                                _parent.search(parseInt(_parent.$('.active').attr('jp-data')));
                                closePage();
                            } else {
                                layer.alert(data.desc, {
                                    icon: 5
                                });
                            }
                        },
                        error: function () {
                            layer.alert('系统错误', {
                                icon: 5
                            });
                        }
                    })
                }
            }
        }
    </script>
</body>
</html>
